import time
import pymssql
#import decimal
class MSSQL:
  def __init__(self,host,user,pwd,db):
    self.host=host
    self.user=user
    self.pwd=pwd
    self.db=db
  def GetConnect(self):
    if not self.db:
      raise(NameError,'没有目标数据库')
    self.connect=pymssql.connect(host=self.host,user=self.user,password=self.pwd,database=self.db,charset='utf8')
    cur=self.connect.cursor()
    if not cur:
      raise(NameError,'数据库访问失败')
    else:
      return cur

  def ExecSql(self,sql):
     cur=self.GetConnect()
     cur.execute(sql)
     self.connect.commit()
     self.connect.close()

  def ExecQuery(self,sql):
    cur=self.GetConnect()
    cur.execute(sql)
    resList = cur.fetchall()
    self.connect.close()
    return resList
  #创建连接
  def createConnect(self):
    self.cursor = self.GetConnect()

  #批量执行，未commit
  def executeBatch(self, sql):
    self.cursor.execute(sql)

  def commit(self):
    self.connect.commit()

  def query(self,sql):
    self.cursor.execute(sql)
    resList = self.cursor.fetchall()
    return resList
  def close(self):
      if self.connect:
        self.connect.close()


def main():
  ms = MSSQL(host="192.168.4.241", user="sa", pwd="jy@88250860", db="EMAS_BASE")
  resList = ms.ExecQuery("select * from TBL_EMAS_VOICEBLACK")
  print(resList)
  resList = ms.ExecQuery("select * from TBL_EMAS_VOICEBLACK")
  print(resList)

  ms.createConnect()
  for i in range(0,10):
    for j in range(0,10):
      # resList = ms.ExecQuery("select * from TBL_EMAS_VOICEBLACK")
      s=str(i*10+j)
      ms.executeBatch("INSERT INTO test1(name,age)values ('name"+s+"',"+s+")")
    ms.commit()

  ms.close()

if __name__ == '__main__':
  main()
  input("执行完成:")
